***********************************************************************************************
* Replication files for "Self-Fulfilling Debt Crises with Long Stagnations"
* by Ayres, Navarro, Nicolini, and Teles.
* November, 2024
* Contact Joao Ayres (jluizayres@gmail.com) or Gaston Navarro (gmnavarro@gmail.com)
* if you have questions.
***********************************************************************************************

We have the following set of codes:

(A) Estimation of output process (folder ReplicationPackage/EstimationOutputProcess)
(B) Codes of the two-period model (folder ReplicationPackage/Model/TwoPeriod)
(C) Codes of the infinite horizon model (folder ReplicationPackage/Model/InfiniteHorizon)

***********************************************************************************************
(A) Estimation of output process (folder ReplicationPackage/EstimationOutputProcess)
. The code MAIN_MCMC.m replicates Table 1: Prior and posterior distributions
. The file Data_MATLAB.xlsx contains the data used for the estimation.
. The files Compute_XIss.m, Kim_Filter_MA.m, and posterior_fun_MA, 
  are auxiliary functions used in the estimation.

(A.1) Estimation of output process with different standard deviations across growth regimes
(folder ReplicationPackage/EstimationOutputProcess/DifferentSigmas)
. The file MAIN_MCMC_sigmas.m replicates 
  Table A.1: Prior and posterior distributions: different σ across growth states.
. The file Data_MATLAB.xlsx contains the data used for the estimation.
. The files Compute_XIss.m, Kim_Filter_MA_sg.m, and posterior_fun_MA, 
  are auxiliary functions used in the estimation.

***********************************************************************************************
(B) Codes of the two-period model (folder ReplicationPackage/Model/TwoPeriod)
. The file TwoStates.m replicates Figures 1 to 5.
. The figures are saved in the folder ReplicationPackage/Model/TwoPeriod/Figures.

***********************************************************************************************
(C) Codes of the infinite horizon model
. The codes to solve and simulate the infinite horizon model were written in Fortran.
. We used the Intel Fortran Compiler Classic (ifort) version 2023.2.1 to run the codes.
. Description of main Fortran files:
	. MAIN.f90: main file that solves the model through value function iteration and call the other functions/modules.
	. mod_parametesr.f90: define most parameters, variables, and grids.
	. ModuleSAVE.f90: save the outputs.
	. ModuleSIMULATION.f90: simulate the model after a solution is found.
	. FUNCTIONS.f90: module containig the functions used to solve the model.
	. sub_discretize.90: discretization of the i.i.d. endowment shock.
	. Toolbox.90 and toolbox_CE.f90: standard toolboxes.
. To upload an initial guess, set load_guess = 1 in file mod_parameters.f90, line 19.
. The codes to generate the figures and tables were written in MATLAB.

(C.1) Argentina (folder ReplicationPackage/Model/InfiniteHorizon/Argentina)
. To run the codes for Argentina, we use the following commands.
	- sbatch compile.sh
	- sbatch -a 1-11 run.sh
. All the cases for Argentina converged without the need to run the code again.
. The solution and simulations of each version of the model are saved in the respective OUTPUT folder. The parameter specification of each case is set in the file MAIN.f90, lines 26 to 36.
. The file Plots_Paper.m replicates Figures 6, 7, 10, 12, E1, F7, and Tables 1 and F.2.

(C.2) Spain (folder ReplicationPackage/Model/InfiniteHorizon/Spain)
. To run the codes for Spain, we use the following commands.
	- sbatch compile.sh
	- sbatch -a 1-13 run.sh
. All the codes for Spain converged without the need to run the code again, except for the following cases:
	. Output 4: high delta (low maturity)
	. Output 8: different default cost in the high-growth state (phi(gH))
	. Output 11: different growth spreads (gH-gL)
	. For these cases, we used the last iteration as initial guess and run the codes multiple times until convergence.
. The solution and simulations of each version of the model are saved in the respective OUTPUT folder. The parameter specification of each case is set in the file MAIN.f90, lines 26 to 38.
. The file Plots_Paper.m replicates Figures 8, 9, 11, F.3, F.4, F.8 and Tables 2, F.1, and F.3.

(C.3) Extras (folder ReplicationPackage/Model/InfiniteHorizon)
. The file ReplicationPackage/Model/InfiniteHorizon/main_IRF_ergodic.m replicates Figures F.6(a) and F.6(b).
	- figures are saved in the folder ReplicationPackage/Model/InfiniteHorizon/Figures/Crisis
. The file ReplicationPackage/Model/InfiniteHorizon/main_IRF.m replicates Figures F.5(a)-(d).
	- the file runs the code for a single country. The country must be specified in line 7 (idcase = 1 for Argentina, and = 2 for Spain)
	- it uses the codes run_sim.m and draw_random_state.m.
	- figures are saved in the folder ReplicationPackage/Model/InfiniteHorizon/Figures/IRF
. The file ReplicationPackage/Model/InfiniteHorizon/main_PARETOFRONTIER.m replicates Figure F.1(a)-(f), and F.2(a)-(b).
	- the file runs the code for a single country. The country must be specified in line 10 (idcase = 1 for Argentina, and = 2 for Spain)
	- figures are saved in the folder ReplicationPackage/Model/InfiniteHorizon/Figures/ParetoFrontier
